package com.fanghaoran.view.user.infoFrame;

import com.fanghaoran.bean.Interaction;
import com.fanghaoran.controller.InfoController;
import com.fanghaoran.entity.Users;
import com.fanghaoran.view.LoginFrame;
import com.fanghaoran.view.user.BackGroundPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 * @Author: win11
 * @Date: 2023/03/21/15:35
 * @Description: 编辑个人基本信息的界面
 */
public class UpdateInfoFrame extends JFrame {
    /**
     * 全局变量记录性别
     */
    private String gender;

    public UpdateInfoFrame() throws Exception {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView();

        //设置可见
        this.setVisible(true);
    }

    private void initView() throws Exception {
        //数据库操作获取该用户的所有信息
        Users user = (Users) new InfoController().getInfo(LoginFrame.usernameInput).getMessage();

        //页面组建
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //垂直box 垂直布局
        Box vBox = Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(50));

        Box box1 = Box.createHorizontalBox();
        //用户名文本提示
        JLabel usernameText = new JLabel("用户名:");
        //用户名
        JTextField username = new JTextField(user.getUsername());
        //保存一下用户名,在后面更改用户名时需要用到
        String oldUsername = user.getUsername();
        //添加到box
        box1.add(usernameText);
        box1.add(Box.createHorizontalStrut(20));
        box1.add(username);


        Box box3 = Box.createHorizontalBox();
        //手机号文本提示
        JLabel telephoneText = new JLabel("手机号:");
        //手机号
        JTextField telephone = new JTextField(user.getTelephone());
        //添加到box
        String oldTelephone = user.getTelephone();
        box3.add(telephoneText);
        box3.add(Box.createHorizontalStrut(20));
        box3.add(telephone);

        Box box4 = Box.createHorizontalBox();
        //昵称文本提示
        JLabel nicknameText = new JLabel("昵    称:");
        //昵称
        JTextField nickname = new JTextField(user.getNickname());
        //添加到box
        box4.add(nicknameText);
        box4.add(Box.createHorizontalStrut(20));
        box4.add(nickname);

        Box box5 = Box.createHorizontalBox();
        //性别文本提示
        JLabel genderText = new JLabel("性    别:");
        //男生选项按钮
        JRadioButton male = new JRadioButton("男", true);
        //女生选项按钮
        JRadioButton female = new JRadioButton("女", false);
        //创建按钮组放入两个按钮->实现单选的效果
        ButtonGroup bg = new ButtonGroup();
        bg.add(male);
        bg.add(female);

        //添加到box
        box5.add(genderText);
        box5.add(Box.createHorizontalStrut(20));
        box5.add(male);
        box5.add(female);


        Box box7 = Box.createHorizontalBox();
        //个性签名文本提示
        JLabel signatureText = new JLabel("个性签名:");
        //个性签名
        JTextArea signature = new JTextArea(user.getSignature());
        //添加到box
        box7.add(signatureText);
        box7.add(Box.createHorizontalStrut(20));
        box7.add(signature);

        //按钮
        Box box8 = Box.createHorizontalBox();
        //返回按钮
        JButton returnButton = new JButton("返回");
        //完成修改按钮
        JButton modify = new JButton("完成修改");
        //添加到box
        box8.add(returnButton);
        box8.add(Box.createHorizontalStrut(20));
        box8.add(modify);
        //按钮添加点击事件
        //性别选框
        male.addActionListener(e -> gender = male.getText());
        female.addActionListener(e -> gender = female.getText());

        //返回
        returnButton.addActionListener(e -> exit());

        //完成修改
        modify.addActionListener(e -> {
                    //获取用户修改后的信息
                    String usernameInput = username.getText();
                    String teleInput = telephone.getText();
                    String nicknameInput = nickname.getText();
                    String genderInput = gender == null ? user.getGender() : gender;
                    String signatureInput = signature.getText();
                    //封装到类中 方便传给controller
                    Users newUser = new Users();
                    newUser.setUsername(usernameInput);
                    newUser.setTelephone(teleInput);
                    newUser.setNickname(nicknameInput);
                    newUser.setGender(genderInput);
                    newUser.setSignature(signatureInput);

                    //用户输入传给controller层
                    //参数一:用户修改后的数据
                    //参数二+参数三:要修改数据的用户的原始用户名和原始手机号
                    Interaction updateInfo = null;
                    try {
                        updateInfo = new InfoController().updateInfo(newUser, oldUsername, oldTelephone);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    if(updateInfo!=null) {
                        //对返回结果进行页面处理
                        if (("用户名为空").equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "用户名不能为空!");
                        } else if ("密码为空".equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "密码不能为空!");
                        } else if ("手机号为空".equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "手机号不能为空!");
                        } else if ("密码不符合规范".equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "密码不符合规范!");
                        } else if ("手机号无效".equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "手机号无效!");
                        } else if ("用户名已存在".equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "用户名已存在!");
                        } else if ("该手机号已绑定其他账号".equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "该手机号已绑定其他账号!");
                        } else if ("生日日期不符".equals(updateInfo.getResult())) {
                            JOptionPane.showMessageDialog(panel, "生日日期错误");
                        } else {
                            JOptionPane.showMessageDialog(panel, "信息编辑成功!");
                            this.dispose();
                        }
                    }
                }
        );

        Box box9 = Box.createHorizontalBox();
        //提示文本
        JLabel tips = new JLabel("温馨提示:编辑完需要重新进入个人信息页面");
        //添加到box
        box9.add(tips);

        //所有水平布局都添加到垂直布局的box
        vBox.add(box1);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box3);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box4);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box5);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box7);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box8);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box9);

        //添加到panel
        panel.add(vBox);
        //添加到frame
        this.add(panel);

    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(500, 500);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    private void exit() {
        this.dispose();
    }
}
